﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using WMS.API.Application.Command.BasicManagement.ShelfManage;
using WMS.Domain.Entities.BasicManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.BasicManagement.ShelfManage
{
    public class ShelfManageModelCommandHandler:IRequestHandler<ShelfManageModelCommand,APIResult<APIPaging<ShelfManageModel>>>
    {
        /// <summary>
        /// 货架管理仓储
        /// </summary>
        IBaseRepository<ShelfManageModel> _shelfManageModelBaseRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="shelfManageModelBaseRepository"></param>
        public ShelfManageModelCommandHandler(IBaseRepository<ShelfManageModel> shelfManageModelBaseRepository)
        {
            _shelfManageModelBaseRepository = shelfManageModelBaseRepository;
        }
        /// <summary>
        /// 货架管理仓储
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<APIPaging<ShelfManageModel>>> Handle(ShelfManageModelCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var shelfManageModel = _shelfManageModelBaseRepository.GetAll();
                shelfManageModel = shelfManageModel.WhereIf(!string.IsNullOrEmpty(request.ShelfName), m => m.ShelfName.Contains(request.ShelfName));
                shelfManageModel = shelfManageModel.WhereIf(!string.IsNullOrEmpty(request.ShelfNumber), m => m.ShelfNumber.Contains(request.ShelfNumber));
                var page = new APIPaging<ShelfManageModel>();
                page.TotalCount = shelfManageModel.Count();
                page.PageData = shelfManageModel.OrderByDescending(m => m.Id).Skip((request.PageIndex - 1) * request.PagePage).Take(request.PagePage).ToList();

                return Task.FromResult(new APIResult<APIPaging<ShelfManageModel>>("货架信息查询成功") { Code=ResultCode.Success,Data= page });
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
